package com.itheima.services;

import com.itheima.mapper.UserMapper;
import com.itheima.pojo.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class UserServicesA implements UserServices{
    @Autowired
    public UserMapper userMapper;
    @Override
    public User Ulogin(User user) {
        User user1=userMapper.Ulogin(user);
        return user1;
    }

    @Override
    public Result registered(User user) {
        if(user.getName().length()>16||user.getName().length()<4||user.getPassword().length()<4||user.getPassword().length()>20){
            return new Result(0,"Error_Rule",null);
        }
        User user1=userMapper.findName(user.getName());
        if(user1!=null){
            return new Result(0,"Exist",null);
        }
        userMapper.registered(user);
        return Result.success();
    }

    @Override
    public String getImage(String userName) {
        String image=userMapper.getImage(userName);
        return image;
    }

    @Override
    public UserMessage getUserMessage(String userName) {
        UserMessage userMessage=userMapper.getUserMessage(userName);
        return userMessage;
    }

    @Override
    public Result changeUserMessage(UserMessage userMessage) {
        if(userMessage.getUserName().length()>16||userMessage.getUserName().length()<4||userMessage.getPassword().length()<4||userMessage.getPassword().length()>20){
            return new Result(0,"Error_Rule",null);
        }
        User user1=userMapper.findName2(userMessage.getUserName(),userMessage.getUserId());
        if(user1!=null){
            return new Result(0,"Exist",null);
        }
        userMapper.changeUserMessage(userMessage);
        return Result.success();
    }

    @Override
    public List<Shopping> uShowFood(Food food,String userName) {
        List<Shopping>foods=userMapper.uShowFood(food.getCategory(),food.getFoodName(),userName);
        return foods;
    }

    @Override
    public void minusFood(String userName, String foodName) {
        userMapper.minusFood(userName,foodName);
    }

    @Override
    public void plusFood(String userName, String foodName) {
          ShoppingList shoppingList=userMapper.findFood(userName,foodName);
          if(shoppingList!=null){
              System.out.println("存在记录");
              userMapper.addCount(userName,foodName);
          }else{
              System.out.println("不存在记录");
              userMapper.initCount(userName,foodName);
          }
    }

    @Override
    public String getList(String userName) {
        List<ShoppingList> shoppingLists=userMapper.getList(userName);
        StringBuilder list =new StringBuilder("");
        for(int i=0;i<shoppingLists.size();i++){
            ShoppingList shoppingList=shoppingLists.get(i);
            String info=shoppingList.getFoodName()+"*"+shoppingList.getCount()+"份"+"\n";
            list.append(info);
        }
        return list.toString();
    }

    @Override
    public Result submitOrder(String userName, String list, String remark) {
        System.out.println(list);
        System.out.println(list==null);
        System.out.println(list.isEmpty());
        if(list.isEmpty()){
            return new Result(0,"No_Food",null);
        }
        String address=userMapper.getAddress(userName);
        System.out.println("address");
        System.out.println(address);
        if(address==null){
            return new Result(0,"No_Address",null);
        }
        userMapper.deleteShoppinglist(userName);
        LocalDateTime localDateTime=LocalDateTime.now();
        userMapper.submitOrder(userName,list,address,remark, localDateTime);
        return Result.success();
    }

    @Override
    public PageBean2 uShowOrders(String userName, int currentPage, int pageSize) {
        int count=userMapper.conCount(userName);
        int start=(currentPage-1)*pageSize;
        List<UserOrders>userOrders=userMapper.conPage(userName,start,pageSize);
        PageBean2 pageBean=new PageBean2(count,userOrders);
        return pageBean;
    }

    @Override
    public void giveRate(Long orderId, int rate) {
        userMapper.giveRate(orderId,rate);
    }

    @Override
    public Result ifCancel(String userName) {
        List<Long> datas=userMapper.ifCancel(userName);
        System.out.println(datas);
        if(!datas.isEmpty()) {
            for (Long orderId : datas) {
                System.out.println(orderId);
                userMapper.knowCancel(orderId);
            }
            return new Result(0, "UNKNOW", null);
        }
        return Result.success();
    }
}
